package com.mengya.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;

import com.mengya.gzcwt.R;
import com.mengya.utils.AndroidInfoUtil;

public class BaseActivity extends Activity {
	public SharedPreferences sp = null;
	public Dialog alert = null;
	public String hardinfo = "";

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		sp = this.getSharedPreferences(Conf.SHARED_PREFERENCE_NAME,
				Context.MODE_PRIVATE);
		sp.edit().putString("hardno", "dfdd98d372745ac").commit();
		hardinfo = sp.getString("hardno", "no");
		if (!sp.getBoolean("invaliduser", false)) {
			String invaliduser = getResources().getString(R.string.invaliduser);
			/**
			 * @step 1 reading hard info; </br>@step 2 reading sp to check
			 *       whether the user is valid or not;</br> step 3 if valid,go
			 *       on; if not to get user info by request the valid interface
			 *       ;
			 */
			// String imei = AndroidInfoUtil.getPhoneInfo(this);
			if ("no".equals(hardinfo)) {
				String imei = AndroidInfoUtil.getUniqueSn(this);
				// Toast.makeText(this, String.format(invaliduser, imei),
				// Toast.LENGTH_LONG).show();
				showToast(String.format(invaliduser, imei));
				sp.edit().putString("hardno", imei).commit();
				sp.edit().putString("user", imei).commit();
			}
		}
	}

	// public void showToast(String str) {
	// Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
	// }
	public void showToast(String str) {
		// Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
		alert = new AlertDialog.Builder(this)
				.setTitle(str)
				.setMessage(str)
				.setPositiveButton(R.string.ok,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								close();
							}
						})
				.setNegativeButton(R.string.cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								close();
							}
						}).show();
	}

	private void close() {
		alert.dismiss();
	}
}
